package com.dbdata.move;

import com.wksc.commons.utils.collections.CollectionUtils;
import com.wksc.commons.utils.string.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 工具类
 *
 * @author Richard
 */
public class Utils {

    private static final Logger L = LoggerFactory.getLogger(Utils.class);

    /**
     * 读取带表头的csv文件
     * @param filePath 文件路径
     * @param charsetName 文件字符编码
     * @return 数据列表
     * @throws IOException
     */
    public static List<String> readCsvFileWithHead(String filePath, String charsetName)
            throws IOException {
        List<String> list = new ArrayList<String>();
        File file = new File(filePath);
        if (!file.isFile()) {
            L.warn("文件不存在或不是标准的文件，路径：{}", file.getAbsolutePath());
            return list;
        }

        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)
                , charsetName));
        String line;
        while ((line = reader.readLine()) != null) {
            list.add(line);
        }
        return list;
    }

    /**
     * 读取不带表头的csv文件
     * @param filePath 文件路径
     * @param charsetName 文件字符编码
     * @return 数据列表
     * @throws IOException
     */
    public static List<String> readCsvFileNoHead(String filePath, String charsetName) throws
            IOException {
        List<String> list = readCsvFileWithHead(filePath, charsetName);
        if (list.size() > 0) {
            list.remove(0);
        }
        return list;
    }

    /**
     * 获取原系统组织id对应新系统组织id的值
     * @param oldOrganizeId 原系统组织id
     * @return 新系统组织id
     */
    public static Long getOrganizeId(String oldOrganizeId) {
        //获取大队
        if (!"".equals(oldOrganizeId)) {
            switch (Integer.parseInt(oldOrganizeId)) {
                case 1:
                    return 7L;
                case 2:
                    return 8L;
                case 3:
                    return 11L;
                case 4:
                    return 12L;
                case 5:
                    return 14L;
                case 6:
                    return 15L;
                case 7:
                    return 17L;
                case 8:
                    return 18L;
                case 9:
                    return 16L;
                case 10:
                    return 9L;
                case 11:
                    return 13L;
                case 12:
                    return 19L;
                case 13:
                    return 20L;
                case 14:
                    return 47L;
                case 15:
                    return 48L;
                case 50:
                    return 10L;
                case 99:
                    return 49L;
                case 100:
                    return 1L;
                case 101:
                    return 2L;
                case 102:
                    return 3L;
                case 103:
                    return 4L;
                case 104:
                    return 5L;
                case 105:
                    return 6L;
                case 140:
                    return 50L;
                case 150:
                    return 51L;
                case 160:
                    return 52L;
                case 200:
                    return 53L;
                case 201:
                    return 55L;
                case 202:
                    return 56L;
                case 203:
                    return 57L;
                case 204:
                    return 58L;
                case 205:
                    return 59L;
                case 255:
                    return 54L;
                default:
                    L.warn("未匹配到对应的组织机构：{}", oldOrganizeId);
                    return 54L;
            }
        }
        L.warn("组织机构信息为空！使用默认的组织机构“其他”");
        // 默认为“其他”
        return 54L;
    }


    public static Long getBaseId(Byte base) {
        switch (base.intValue()) {
            case 0:
                return 4L;
            case 13:
                return 1L;
            case 101:
                return 1L;
            case 102:
                return 2L;
            case 103:
                return 3L;
            case 104:
                return 4L;
            case 105:
                return 5L;
            case 106:
                return 6L;
            case 150:
                return 7L;
            default:
                return null;
        }
    }

    public static List<String> readErrorFile(String dirName) throws IOException {
        List<String> list = new ArrayList<String>();
        File dir = new File(dirName);
        if (!dir.isDirectory()) {
            L.warn("文件夹不存在，路径：{}", dir.getAbsolutePath());
            return list;
        }

        File[] files = dir.listFiles();
        if (CollectionUtils.isNull(files)) {
            L.warn("文件夹不存在错误文件，路径：{}", dir.getAbsolutePath());
            return list;
        }

        for (File file : files) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)
                    , "UTF-8"));
            String line;
            while ((line = reader.readLine()) != null) {
                String[] errors = line.split(",");
                for (String error : errors) {
                    list.add(error);
                }
            }
        }

        return list;
    }
}
